package terminal.ui.contexts;

import kret.ui.AbstractFrameContext;
import terminal.interfaces.ISerialModel;
import kret.ui.ActionManager;
import terminal.ui.forms.DialogPortOpen;
import kret.ui.model.StringComboBoxModel;

/**
 *
 * @author P. Podwapinski
 */
public class DialogPortOpenContext extends AbstractFrameContext 
{
    public DialogPortOpenContext( ActionManager actionManager, DialogPortOpen frame, ISerialModel serialModel )
    {
        super( actionManager, frame );        
        
        m_comPortModel = new StringComboBoxModel();
        m_baudRateModel = new StringComboBoxModel();
        m_ParityModel = new StringComboBoxModel();
        m_StopBitsModel = new StringComboBoxModel();        
        
        m_serialModel = serialModel;
        
        m_dialogPortOpen = frame;
        
        if ( null != m_dialogPortOpen )
        {
            m_dialogPortOpen.setComPortComboModel( m_comPortModel );
            m_dialogPortOpen.setBaudRateComboModel( m_baudRateModel ); 
            m_dialogPortOpen.setParityComboModel( m_ParityModel );
            m_dialogPortOpen.setStopBitsComboModel( m_StopBitsModel );
        }
        
        bindActions();
        updateContent();
    }
    
    
    @Override public void onFrameClosed()
    {
        
    }
    
    @Override public void onFrameOpened()
    {
        updateContent();
    }
    
    @Override public void onFrameActivated()
    {
        updateContent();
    }
    
    @Override public void onCommandExecuted( final String command )
    {
        if ( null != command )
        {
            switch ( command )
            {                    
                case COMMAND_DIALOG_OPENPORT_CLOSE:
                {
                    closeFrame();
                }
                break;

                case COMMAND_PORT_OPEN:
                {
                    final String comPort = m_comPortModel.getSelectedItem();
                    final String baudRate = m_baudRateModel.getSelectedItem();
                    
                    if ( m_serialModel.getSerialPort().open( comPort, baudRate ) )
                    {
                        setWidgetsByPortState( true );
                        closeFrame();
                    }
                    else
                    {
                        showErrorMessage( "Error", "Can't open " + comPort + "!");
                    }
                }
                break;   
                    
            }            
        }        
    }
       
    private void bindActions()
    {       
        getCommandAdapter().bindToCommand( m_dialogPortOpen.getTriggerOpenPort(), COMMAND_PORT_OPEN );
        getCommandAdapter().bindToCommand( m_dialogPortOpen.getTriggerClose(), COMMAND_DIALOG_OPENPORT_CLOSE );
    }
    
    private void updateContent()
    {          
        if ( null != m_serialModel )
        {
            final String enumeratedPorts[] = m_serialModel.getSerialConfiguration().getEnumeratedPorts();
            final String baudRates[] = m_serialModel.getSerialConfiguration().getBaudRates();
            final String parity[] = m_serialModel.getSerialConfiguration().getParity();
            final String stopBits[] = m_serialModel.getSerialConfiguration().getStopBits();
            
            final boolean portOpened = m_serialModel.getSerialPort().isOpened();
            
            if ( !portOpened )
            {
                if ( 0 != enumeratedPorts.length )
                {
                    m_comPortModel.setData( enumeratedPorts );
                    m_comPortModel.setSelectedItem( enumeratedPorts[ 0 ] );

                    m_baudRateModel.setData( baudRates );                
                    m_baudRateModel.setSelectedItem( baudRates[ 0 ] );
                    
                    m_ParityModel.setData( parity );
                    m_ParityModel.setSelectedItem( parity[ 0 ] );
                    
                    m_StopBitsModel.setData( stopBits );
                    m_StopBitsModel.setSelectedItem( stopBits[ 0 ] );
                }                
            }
            else
            {
                m_comPortModel.setSelectedItem( m_serialModel.getSerialPort().getPortName() );
                m_baudRateModel.setSelectedItem( m_serialModel.getSerialPort().getBaudrate() );
            }
                        
            setWidgetsByPortState( portOpened );
        }
    }
    
    private void setWidgetsByPortState( boolean portOpened )
    {      
        getCommandAdapter().setActionEnabledByCommand( COMMAND_PORT_CLOSE, portOpened );
        getCommandAdapter().setActionEnabledByCommand( COMMAND_DIALOG_OPENPORT_SHOW, !portOpened);
        getCommandAdapter().setActionEnabledByCommand( COMMAND_PORT_OPEN, !portOpened ); 
        
        m_dialogPortOpen.setPortConfigurationEnabled( !portOpened );
    }
        
    private StringComboBoxModel m_comPortModel;    
    private StringComboBoxModel m_baudRateModel;
    private StringComboBoxModel m_ParityModel;
    private StringComboBoxModel m_StopBitsModel;
    
    private ISerialModel m_serialModel;    
    private DialogPortOpen m_dialogPortOpen;
}
